<?php
	
	/*
	 * 用户主页控制器
	 */
	class HomeController extends CommonController{
		
		public function index(){
			$uid = isset($_GET['uid'])?(int)$_GET['uid']:1;
			//返回当前UID的详细数据
			$userinfo = $this->_userInfo($uid);
			$this->assign('userinfo',$userinfo);
			$this->assign('lv',$this->getlevel($userinfo['exp']));
			//采纳率
			$cnl ='';
			if($userinfo['answer']==0){
				if($cnl==0) $cnl=0;	
			}else{
				$cnl = round($userinfo['accept']/$userinfo['answer']*100);
			}
			
			$this->assign('cnl',$cnl.'%');
			//返回这个用户发表的提问
			$this->assign('myask',$this->_myask($uid));
			//返回这个用户发表的回答
			$this->assign('myanswer',$this->_myanswer($uid));
			//分配顶级分类  分配所有问题的总数
			$this->getHeadData();
			$this->display();
		}
		
		/**
		 * home方法主页的首页显示的信息
		 */
		 public function home(){
		 	$uid = isset($_GET['uid'])?(int)$_GET['uid']:1;
			//返回当前UID的详细数据
			$userinfo = $this->_userInfo($uid);
			//等级
			$this->assign('lv',$this->getlevel($userinfo['exp']));
			//采纳率
			$this->assign('cnl',round($userinfo['accept']/$userinfo['answer']*100).'%');
			$this->assign('userinfo',$userinfo);
			//返回这个用户发表的提问
			$this->assign('myask',$this->_myask($uid,8));
			//返回这个用户发表的回答
			$this->assign('myanswer',$this->_myanswer($uid));
			$this->display();
		 }
		
		/**
		 * myask我的问题页面显示
		 */
		 public function myask(){
		 	$uid = isset($_GET['uid'])?(int)$_GET['uid']:1;
			//状态表示 默认查询没有解决的问题
			$slove = isset($_GET['slove'])?(int)$_GET['slove']:0;
			//分配 变量给模板
			$this->assign('myask',$this->_myask($uid,null,$slove));
//			var_dump($this->_myask($uid,null,$slove));
//			var_dump($this->_myask($uid));
			$this->display();
		 }
		 /**
		  * myanswer我回答的问题  显示所有 我回答过的提问
		  */
		  public function myanswer(){
		  	$uid = isset($_GET['uid'])?(int)$_GET['uid']:1;
			$accept = isset($_GET['accept'])?(int)$_GET['accept']:0;
			$this->assign('accept',$accept);
			//判断显示为已经采纳的还是全部
			if($accept==1){
				$accept = ' AND an.accept='.$accept;
			}else{
				$accept='';
			}
			
			$sql = "SELECT an.uid,a.content, a.asid, a.time, c.title, a.answer FROM hd_answer AS an JOIN hd_ask AS a JOIN hd_category AS c ON an.asid = a.asid AND c.cid = a.cid WHERE an.uid = {$uid} {$accept} GROUP BY asid ORDER BY time DESC";			
			$model = M('ask');
			$myanswer = $model->query($sql);
//			var_dump($myanswer);
			$this->assign('myanswer',$myanswer);
		  	$this->display();
		  }
		/**
		 * 头像上传
		 */
		 public function myface(){
		 	//上传头像
		 	if(IS_POST){
		 		$upload = new Upload();
				$upload->upFile();
				if($upfilepath = '/'.ltrim($upload->upFiles[0],APP_PUBLIC_PATH)){
					$model = D('user');
					$uid = session('uid');
					$model->_exec("UPDATE hd_user SET face='{$upfilepath}' WHERE uid={$uid}");
					$this->success('头像上传成功',__APP__.'?c=home&uid='.session('uid'));
				}else{
					$this->error('头像上传失败可能是文件格式或大小不正确',__APP__.'?c=home&uid='.session('uid'));
				}
				
				
		 	}
		 	//先查询原来的头像
		 	$userinfo = $this->_prvuser();
			if(session('uid')!=$userinfo['uid']){
				$this->error('请您先登陆',__APP__);
				die;
			}
			$this->assign('face',$userinfo['face']);
		 	$this->display();
		 }
		/**
		 * 从数据库中查询出当前uid的提问
		 * $slove是否已经解决
		 * $limit 是否限制条数
		 */
		private function _myask($uid,$limit=null,$solve=null){
			//判断是否进行限制条数
			$limit = is_null($limit)?8:$limit;
			//如果限制条数那么使用这个LIMIT组合
			$limit = " LIMIT ".$limit;
			//已经解决的问题
			if($solve){
				//已经解决的问题
				$solve = " AND ask.solve=1";	
			}else{
				//没解决的问题
				$solve = " AND ask.solve=0";
			}
			$model = M('ask');
//			$myask = $model->query("SELECT solve,an.asid,ask.content,c.title,ask.answer,an.time FROM hd_ask as ask JOIN hd_user as u JOIN hd_category as c JOIN hd_answer as an ON u.uid = ask.uid AND c.cid=ask.cid AND an.asid=ask.asid WHERE u.uid={$uid} {$solve} GROUP BY ask.asid ORDER BY an.time DESC {$limit}");
			$myask = $model->query("SELECT ask.time,ask.asid, ask.solve, ask.content, c.title, ask.answer FROM hd_ask AS ask JOIN hd_user AS u JOIN hd_category AS c ON u.uid = ask.uid AND c.cid = ask.cid WHERE u.uid = {$uid} {$solve} {$limit}");
			return $myask;	
		}
		
		/**
		 * 查询当前用户的详细 信息
		 */
		 private function _userInfo($uid){
		 	$model = D('user');
			$userinfo = current($model->getUserInfo($uid));
//			var_dump($userinfo);
			return $userinfo;
		 }
		 
		 /**
		  * 查询当前用户的所有回答
		  */
		 private function _myanswer($uid){
		 	$model = M('ask');
			$myask = $model->query('SELECT an.time,an.asid, ask.content, c.title, ask.answer, an.uid FROM hd_ask AS ask JOIN hd_category AS c JOIN hd_answer AS an ON an.uid = ask.uid AND c.cid = ask.cid AND an.asid = ask.asid WHERE an.uid = '.$uid.' GROUP BY asid');
			return $myask;	
		 }
		 
		 /**
		  * 我的等级方法
		  */
		  public function mylevel(){
		  	$userinfo = $this->_prvuser();
		  	//等级
			$this->assign('lv',$this->getlevel($userinfo['exp']));
			//经验
			$this->assign('exp',$userinfo['exp']);
			$this->assign('rule',$this->getRule());
		  	$this->display();
		  }
		  
		  /**
		   * 我的金币
		   */
		   public function mygold(){
		   	$userinfo = $this->_prvuser();
			$this->assign('rule',$this->getRule());
			$this->assign('gold',$userinfo['point']);
		   	$this->display();
		   }
			
	       /**
		    * 查询当前UID的用户信息
		    */
		    private function _prvuser(){
		    	$model = D('user');
		   		$uid = isset($_GET['uid'])?$_GET['uid']:0;
				if(!$uid){
					die;
				}
				$userinfo = current($model->getUserInfo($uid));
				return $userinfo;
		    }
	}

?>
